/**
 * @Project POL3Cay
 * @Author Tran Manh Hung
 * @Email: hungtmit@gmail.com | tmh@xwebgate.com
 * @Copyright © 2011 TMH. All rights reserved
 * @Createdate Feb 18, 2011
 * @Createtime 1:13:45 PM
 */

package models.card
{
	import events.BaCayEvent;

	public class BaCayHand extends BaseHand
	{
		private var _rank:int;

		private var _status:String;

		private var _suitRank:int;

		public function BaCayHand()
		{
			super();

			reset();
		}

		override public function toString():String
		{
			var ret:String = "";

			for each (var card:Card in _cardsSet)
			{
				ret += card.toString() + " ";
			}

			ret += "Điểm: " + _rank.toString() + " " + status.toString();

			return ret;
		}

		public function reset():void
		{
			_rank = 0;
			_status = "";
			_suitRank = 0;
		}

		/**
		 * turn: from 1
		 * _cardsSet index from 0!!!
		 */
		public function receiveCard(card:Card, currentTurn:int):void
		{
			_cardsSet[currentTurn - 1] = card;

			if (card.value == 1 && card.suit == Card.DIAMONDS)
			{
				_suitRank = 1;
			}

			_rank = (_rank + card.value) % 10;

			if (_rank == 0)
			{
				_rank = 10;
			}

			var e:BaCayEvent = new BaCayEvent(BaCayEvent.RECEIVE_CARD, {card:card,turn:currentTurn});
			dispatchEvent(e);
		}

		public function get status():String
		{
			switch (_rank)
			{
				case 1:
					_status = "Tịt";
					break;
				case 4:
					_status = "Còn non";
					break;
				case 5:
					_status = "Nửa đời";
					break;
				case 6:
					_status = "Đã già";
					break;
				case 10:
					_status = "Già làng";
					break;
				default:
					_status = "";
					break;
			}

			if (_suitRank == 1)
			{
				switch (_rank)
				{
					case 1:
						_status = "Tịt CỤ";
						break;
					case 2:
					case 3:
					case 4:
					case 5:
					case 6:
					case 7:
					case 8:
					case 9:
						_status = _rank.toString() + " CỤ";
						break;
					case 10:
						_status = "Mười CỤ";
						break;
					default:
						_status = "";
						break;
				}
			}

			return _status;
		}

		public function get rank():int
		{
			return _rank;
		}
	}
}